package com.yugao.fintech.antelope.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.antelope.system.assembly.RamModuleAssembly;
import com.yugao.fintech.antelope.system.manager.RamModuleManager;
import com.yugao.fintech.antelope.system.manager.SysMqManager;
import com.yugao.fintech.antelope.system.mapper.RamModuleMapper;
import com.yugao.fintech.antelope.system.model.entity.RamModule;
import com.yugao.fintech.antelope.system.model.rqrs.ram.RamModuleListReq;
import com.yugao.fintech.antelope.system.model.rqrs.ram.RamModuleUpdateReq;
import com.yugao.fintech.antelope.system.service.RamModuleService;
import com.yugao.fintech.framework.assistant.utils.StringUtils;
import com.yugao.fintech.framework.core.annotations.FieldUniqueCheck;
import com.yugao.fintech.framework.core.enums.OperationTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.List;


@Service
public class RamModuleServiceImpl extends ServiceImpl<RamModuleMapper, RamModule> implements RamModuleService {
    @Autowired
    private RamModuleManager ramModuleManager;
    @Autowired
    private RamModuleAssembly ramModuleAssembly;
    @Autowired
    private SysMqManager mqManager;


    @Override
    public List<RamModule> listResourceModule(RamModuleListReq rq) {
        LambdaQueryWrapper<RamModule> lqw = RamModule.lqw()
                .like(StringUtils.isNotEmpty(rq.getName()), RamModule::getName, rq.getName())
                .like(StringUtils.isNotEmpty(rq.getCode()), RamModule::getCode, rq.getCode())
                .orderByDesc(RamModule::getUpdatedAt);
        return this.list(lqw);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @FieldUniqueCheck(type = OperationTypeEnum.SAVE)
    public boolean saveResourceModule(RamModule rq) {
        boolean ret = this.save(rq);
        List<RamModule> allModule = this.list();
        // 发送刷新消息. 注意，需要事务提交后，在进行发送刷新消息。如果数据库出现异常提交失败, 这时做刷新缓存或者通知处理, 很有可能有问题
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                mqManager.sendResourceModuleRefresh(allModule);
            }
        });
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateResourceModule(RamModuleUpdateReq rq) {
        RamModule entity = ramModuleAssembly.toModule(rq);
        boolean ret = ramModuleManager.updateModuleById(entity);

        List<RamModule> allModule = this.list();
        // 发送刷新消息. 注意，需要事务提交后，在进行发送刷新消息。如果数据库出现异常提交失败, 这时做刷新缓存或者通知处理, 很有可能有问题
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                mqManager.sendResourceModuleRefresh(allModule);
            }
        });
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteResourceModuleByIds(List<Long> ids) {
        boolean ret = this.removeByIds(ids);

        List<RamModule> allModule = this.list();
        // 发送刷新消息. 注意，需要事务提交后，在进行发送刷新消息。如果数据库出现异常提交失败, 这时做刷新缓存或者通知处理, 很有可能有问题
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                mqManager.sendResourceModuleRefresh(allModule);
            }
        });
        return ret;
    }
}
